package com.palrevo.beackBeans.adver;

import java.io.File;
import java.io.IOException;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.palrevo.beackBeans.BeansBase;
import com.palrevo.common.Constants;
import com.palrevo.data.models.Advertisement;
import com.palrevo.utils.Configs;
import com.palrevo.utils.WebUtils;

/**
 * 
 * @author AAsha
 *
 */
@ManagedBean(name="advsBean")
@SessionScoped
@SuppressWarnings("serial")
public class AdvertisementsBean extends BeansBase{
	
	//Constants
	private static final String ADV_ID = "ADV_ID";
	private static final String FILE_NAME = "FILE_NAME";
	private static final String PDF = "pdf";
	
	//Fields
	private Advertisement advertisement = new Advertisement();
	
	public AdvertisementsBean() {
		super();
		advertisement = WebUtils.getApplicationBean().getAdversMap().get(1L);
	}
	
	public void openAdvertisement(){
		String advrId = WebUtils.getRequest().getParameter(ADV_ID);
		if(advrId == null){
			return;
		}
		long id = Long.valueOf(advrId);
		advertisement = WebUtils.getApplicationBean().getAdversMap().get(id);
	}
	
	public void downloadMAdvPDFFile() throws IOException{
		String filesDir = Configs.getConfigValue(Constants.Configs.M_ADVS_FILES_DIR).concat(File.separator).concat(PDF).concat(File.separator);
		downloadPDFFile(filesDir);
	}
	
	public void downloadSAdvPDFFile() throws IOException{
		String filesDir = Configs.getConfigValue(Constants.Configs.S_ADVS_FILES_DIR).concat(File.separator).concat(PDF).concat(File.separator);
		downloadPDFFile(filesDir);
	}
	
	public void downloadPDFFile(String filesDir) throws IOException{
		
		String fileName = WebUtils.getRequest().getParameter(FILE_NAME);
		if(fileName == null){
			return;
		}
		
        File file = new File(filesDir, fileName.toString());
        
        if(file.exists()){
        	WebUtils.downloadFile(file);
        }
	}

	public Advertisement getAdvertisement() {
		return advertisement;
	}

	public void setAdvertisement(Advertisement advertisement) {
		this.advertisement = advertisement;
	}
}
